/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.service.UserDefineService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 用户全局配置Controller
 * @author Jakemanse
 * @version 2013-10-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/userDefine")
public class UserDefineController extends BaseController {

	@Autowired
	private UserDefineService userDefineService;
	
	@ModelAttribute
	public UserDefine get(@RequestParam(required=false) Long id) {
		if (id != null){
			return userDefineService.get(id);
		}else{
			return new UserDefine();
		}
	}
	
	@RequiresPermissions("wechat:userDefine:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserDefine userDefine, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			userDefine.setUser(user);
		}
        Page<UserDefine> page = userDefineService.find(new Page<UserDefine>(request, response), userDefine); 
        model.addAttribute("page", page);
		return "modules/wechat/userDefineList";
	}

	@RequiresPermissions("wechat:userDefine:view")
	@RequestMapping(value = "form")
	public String form(UserDefine userDefine, Model model) {
		
		User user = UserUtils.getUser();
		UserDefine temp = userDefineService.getByUserId(user.getId());
		
		if(temp!=null && !user.isAdmin()){
			userDefine = temp;
		} 
		
		if(userDefine.getUser()==null || userDefine.getUser().getId() == null){
			userDefine.setUser(user);
		}
		
		model.addAttribute("userDefine", userDefine);
		return "modules/wechat/userDefineForm";
	}

	@RequiresPermissions("wechat:userDefine:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("regPicFile") MultipartFile regPicFile,
			@RequestParam("guestBookFile") MultipartFile guestBookFile,
			@RequestParam("signPicFile") MultipartFile signPicFile,
			UserDefine userDefine, Model model,HttpServletRequest request,  RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userDefine)){
			return form(userDefine, model);
		}
		
		
		if(regPicFile != null && regPicFile.getOriginalFilename().length()>4){
			String regMemberPicPath = WechatUtils.getWechatRegMemberSavePath(request)+File.separator;
			FileUtils.createDirectory(regMemberPicPath);
			String sname = regPicFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_reg_"+fileType;
			File file = new File(regMemberPicPath+fname);
			System.out.println("保存文件:"+regMemberPicPath+fname);
			try {
				regPicFile.transferTo(file);
				userDefine.setRegMemberPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+regMemberPicPath+fname);
				e.printStackTrace();
			}
		}
		
		//留言板图文
		if(guestBookFile != null && guestBookFile.getOriginalFilename().length()>4){
			String regMemberPicPath = WechatUtils.getWechatRegMemberSavePath(request)+File.separator;
			FileUtils.createDirectory(regMemberPicPath);
			String sname = guestBookFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_guestbook_"+fileType;
			File file = new File(regMemberPicPath+fname);
			System.out.println("保存文件:"+regMemberPicPath+fname);
			try {
				guestBookFile.transferTo(file);
				userDefine.setGuestBookPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+regMemberPicPath+fname);
				e.printStackTrace();
			}
		}
		
		//签到图文
		if(signPicFile != null && signPicFile.getOriginalFilename().length()>4){
			String regMemberPicPath = WechatUtils.getWechatRegMemberSavePath(request)+File.separator;
			FileUtils.createDirectory(regMemberPicPath);
			String sname = signPicFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_signup_"+fileType;
			File file = new File(regMemberPicPath+fname);
			System.out.println("保存文件:"+regMemberPicPath+fname);
			try {
				signPicFile.transferTo(file);
				userDefine.setSignupPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+regMemberPicPath+fname);
				e.printStackTrace();
			}
		}
		
		userDefineService.save(userDefine);
		
		addMessage(redirectAttributes, "保存用户全局配置'" + userDefine.getName() + "'成功");
		User user = UserUtils.getUser();
		if(user.isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/userDefine/?repage";
		}else{
			return form(userDefine, model);
		}
	}
	
	@RequiresPermissions("wechat:userDefine:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		userDefineService.delete(id);
		addMessage(redirectAttributes, "删除用户全局配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/userDefine/?repage";
	}

}
